<?php
/**
 * Created by PhpStorm
 * User: Jhinwy
 * Date: 6/6/24
 * Time: 10:30 AM
 */

namespace App\Services;

use App\Models\WechatExtContactWelcome;

class ExtContactService
{

    /**
     * 查看欢迎语
     *
     * @param $params
     * @return array|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function showWelcome($params)
    {
        $welcome = WechatExtContactWelcome::query()
            ->where('corp_id', $params['corp_id'] ?? '')
            ->where('user_id', $params['user_id'] ?? '')
            ->first();
        if (!$welcome) {

            return [];
        }

        $welcome->attachments = json_decode($welcome->attachments, true);

        return $welcome;
    }


    /**
     * 更新欢迎语
     *
     * @param $params
     * @return bool
     */
    public function saveWelcome($params)
    {
        $corpId      = $params['corp_id'] ?? '';
        $userId      = $params['user_id'] ?? '';
        $content     = $params['content'] ?? '';
        $attachments = $params['attachments'] ?? [];

        $welcome              = WechatExtContactWelcome::query()->firstOrNew(['corp_id' => $corpId, 'user_id' => $userId]);
        $welcome->content     = $content;
        $welcome->attachments = json_encode($attachments, JSON_UNESCAPED_UNICODE);

        return $welcome->save();
    }
}
